package com.hualan.sfzy.controller.ssh;

import com.hualan.sfzy.constant.SystemCode;
import com.hualan.sfzy.model.entity.Answer;
import com.hualan.sfzy.model.entity.PsychologyTest;
import com.hualan.sfzy.model.entity.PsychologyTestClassify;
import com.hualan.sfzy.service.impl.PsychologyTestClassifyServiceImpl;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/TestClassify")
public class psychologyTestClassifyController {
    @Autowired
    private PsychologyTestClassifyServiceImpl Service;

    /*添加*/
    @PostMapping("/add")
    public Result add(@RequestBody PsychologyTestClassify entity) {
        int i = Service.add(entity);
        //获取添加之后的id
        Integer newId = entity.getId();
        if (i > 0) {
            return Result.success(newId);
        }
        return Result.error();
    }

    /*编辑*/
    @PutMapping("/edit")
    public Result edit(@RequestBody PsychologyTestClassify entity) {
        int i = Service.edit(entity);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }

    /*删除*/
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id) {
        int i = Service.remove(id);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error();
    }

    /*批量删除*/
    @DeleteMapping("/delBatch")
    public Result delBatch(Integer[] ids) {
        // 参数校验
        if (ids == null || ids.length == 0) {
            return Result.error("请提供有效的删除 ID 列表");
        }
        int i = Service.removeBatch(ids);
        if (i > 0) {
            return Result.success(i);
        }
        return Result.error("未删除任何记录");

    }

    /*查询所有*/
    @GetMapping("/find")
    public Result find() {
        List<PsychologyTestClassify> list = Service.find();
        if (list.size() > 0) {
            return Result.success(list);
        }
        return Result.error();
    }

    /*分页查询所有*/
    @GetMapping("/findPage")
    public Result findPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                           PsychologyTestClassify entity) {
        Map<String, Object> map = Service.findPage(pageNum, pageSize, entity);
        if (map != null && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.error();
    }

    /*根据id查询单个*/
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        PsychologyTestClassify psychologyTestClassify = Service.findById(id);
        if (psychologyTestClassify != null) {
            return Result.success(psychologyTestClassify);
        }
        return Result.error();
    }

    /*查出所有的分类名*/
    @GetMapping("/findAllName")
    public Result findAllName() {
        List<Map<String, Object>> list = Service.selectAllClassifyName();
        if (list != null && !list.isEmpty()) {
            return Result.success(list);
        }
        return Result.error();
    }

    /*根据分类名查询并且分页*/
    @GetMapping("/findByName")
    public Result findByName(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                             @RequestParam("classifyName") String classifyName) {
        Map<String, Object> map = Service.selectByClassifyName(pageNum, pageSize, classifyName);
        if (map != null && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.error();
    }

/*根据分类id 查询test总数量*/
    @GetMapping("/findTestCount/{id}")
    public Result findTestCount(@PathVariable("id") Integer id) {
        Integer count = Service.selectByClassifyId(id);
        if (count != null) {
            return Result.success(count);
        }
        return Result.error();
    }

    /*根据分类ID获取该分类下的所有测试*/
    @GetMapping("/findTestsByClassifyId/{id}")
    public Result findTestsByClassifyId(@PathVariable("id") Integer id) {
        List<PsychologyTest> list = Service.selectTestsByClassifyId(id);
        if (list != null && !list.isEmpty()) {
            return Result.success(list);
        }
        return Result.error();
    }


}
